//
//  DLCChooseCityView.m
//  DGGCustomer
//
//  Created by gogogo on 2019/12/19.
//  Copyright © 2019 dgg_lyb. All rights reserved.
//

#import "DLCChooseCityView.h"
#import "DLCChooseCityCollectionViewCell.h"
#import "DLChooseCityModel.h"
#define kContentHeight 280

@interface DLCChooseCityView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topNabarConstraint;

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backViewHeight;
@property (strong, nonatomic) NSArray <DLChooseCityModel *>*cityArray;
@property (weak, nonatomic) IBOutlet UIView *topNaView;
@property (weak, nonatomic) IBOutlet UIView *bgView;


@end

@implementation DLCChooseCityView

+ (instancetype)showWithTitle:(NSString *)title cityArray:(NSArray <DLChooseCityModel *>*)cityArray {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLCChooseCityView class]] pathForResource:@"ChooseCollctionMenuBundle" ofType:@"bundle"]];
    DLCChooseCityView *chooseCityView = [bundle loadNibNamed:NSStringFromClass(DLCChooseCityView.class) owner:DLCChooseCityView.class options:nil].firstObject;
    

    chooseCityView.collectionView.delegate = chooseCityView;
    chooseCityView.collectionView.dataSource = chooseCityView;
    chooseCityView.collectionView.showsVerticalScrollIndicator = NO;
    [chooseCityView.collectionView registerClass:[DLCChooseCityCollectionViewCell class] forCellWithReuseIdentifier:@"DLCChooseCityCollectionViewCell"];
    chooseCityView.titleLab.text = title;
    
    if (kDevice_Is_iPhoneX) {
        chooseCityView.topNabarConstraint.constant = 44;
    }else{
        chooseCityView.topNabarConstraint.constant = 20;
    }
    
    NSString *firstNum = [NSString stringWithFormat:@"%f",(SCREEN_WIDTH - 64)];
    NSString *secondNum = [NSString stringWithFormat:@"%d",3];
    NSDecimalNumber *oneNum = [NSDecimalNumber decimalNumberWithString:firstNum];
    NSDecimalNumber *twoNum = [NSDecimalNumber decimalNumberWithString:secondNum];
    NSDecimalNumber *division= [oneNum decimalNumberByDividingBy:twoNum];
    CGFloat itemWith = division.floatValue;
    
    chooseCityView.layout.itemSize = CGSizeMake(itemWith, 36);
    chooseCityView.layout.minimumInteritemSpacing = 12;
    chooseCityView.layout.minimumLineSpacing = 12;
    chooseCityView.cityArray = cityArray;
    
    chooseCityView.topNaView.backgroundColor =[[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x232324]];
    chooseCityView.bgView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x232324]];
    chooseCityView.collectionView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x232324]];
    [chooseCityView.cancelBtn setImage:[[DLThemeAdapter shareInstance] dynamicImage:[UIImage imageNamed:@"ChooseCollctionMenuBundle.bundle/img_nav_close"] darkImage:[UIImage imageNamed:@"ChooseCollctionMenuBundle.bundle/img_nav_close_slices"]] forState:UIControlStateNormal];
    chooseCityView.titleLab.textColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x333333] darkColor:[UIColor colorWithHex:0xE0E0E0]];
    
    chooseCityView.backViewHeight.constant = kContentHeight;
    chooseCityView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    chooseCityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:chooseCityView];
    chooseCityView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        chooseCityView.alpha = 1;
    } completion:nil];
    return chooseCityView;
}

+ (instancetype)showWithTitle:(NSString *)title cityArray:(NSArray <DLChooseCityModel *>*)cityArray customHeight:(CGFloat)customHeight; {
    
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[DLCChooseCityView class]] pathForResource:@"ChooseCollctionMenuBundle" ofType:@"bundle"]];
    DLCChooseCityView *chooseCityView = [bundle loadNibNamed:NSStringFromClass(DLCChooseCityView.class) owner:DLCChooseCityView.class options:nil].firstObject;
    
    chooseCityView.collectionView.delegate = chooseCityView;
    chooseCityView.collectionView.dataSource = chooseCityView;
    chooseCityView.collectionView.showsVerticalScrollIndicator = NO;
    [chooseCityView.collectionView registerClass:[DLCChooseCityCollectionViewCell class] forCellWithReuseIdentifier:@"DLCChooseCityCollectionViewCell"];
    chooseCityView.titleLab.text = title;
    
    if (kDevice_Is_iPhoneX) {
        chooseCityView.topNabarConstraint.constant = 44;
    }else{
        chooseCityView.topNabarConstraint.constant = 20;
    }
    
    NSString *firstNum = [NSString stringWithFormat:@"%f",(SCREEN_WIDTH - 64)];
    NSString *secondNum = [NSString stringWithFormat:@"%d",3];
    NSDecimalNumber *oneNum = [NSDecimalNumber decimalNumberWithString:firstNum];
    NSDecimalNumber *twoNum = [NSDecimalNumber decimalNumberWithString:secondNum];
    NSDecimalNumber *division= [oneNum decimalNumberByDividingBy:twoNum];
    CGFloat itemWith = division.floatValue;
    
    chooseCityView.layout.itemSize = CGSizeMake(itemWith, 36);
    chooseCityView.layout.minimumInteritemSpacing = 12;
    chooseCityView.layout.minimumLineSpacing = 12;
    chooseCityView.cityArray = cityArray;
    
    chooseCityView.topNaView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
   chooseCityView.bgView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
   chooseCityView.collectionView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
  [chooseCityView.cancelBtn setImage:[[DLThemeAdapter shareInstance] dynamicImage:[UIImage imageNamed:@"ChooseCollctionMenuBundle.bundle/img_nav_close"] darkImage:[UIImage imageNamed:@"ChooseCollctionMenuBundle.bundle/img_nav_close_slices"]] forState:UIControlStateNormal];
   chooseCityView.titleLab.textColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x333333] darkColor:[UIColor colorWithHex:0xE0E0E0]];
    
    chooseCityView.backViewHeight.constant = customHeight;
    chooseCityView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    chooseCityView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:chooseCityView];
    chooseCityView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        chooseCityView.alpha = 1;
    } completion:nil];
    return chooseCityView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.cityArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DLCChooseCityCollectionViewCell  *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DLCChooseCityCollectionViewCell" forIndexPath:indexPath];
    [cell setCornerRadius:self.radius];
    cell.titleLabel.text = self.cityArray[indexPath.row].name;
    DLChooseCityModel *model = self.cityArray[indexPath.row];
    cell.titleLabel.textColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0x333333] darkColor:[UIColor colorWithHex:0xE0E0E0]];
    cell.contentView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0xF7F7F7] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    cell.layer.borderWidth = 0.0;
    cell.titleLabel.font = [UIFont systemFontOfSize:14];
    if (model.status == 1) {
        cell.titleLabel.textColor = [UIColor colorWithHex:0x1CBEBC];
        cell.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
        cell.layer.borderWidth = 1.0;
        cell.layer.borderColor = [UIColor colorWithHex:0x1CBEBC].CGColor;
        cell.contentView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0xE1FCF9] darkColor:[UIColor colorWithHex:0x385857]];
    }
    if (model.status == 2) {
        cell.titleLabel.textColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0xAAAAAA] darkColor:[UIColor colorWithHex:0x636363]];
        cell.contentView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor colorWithHex:0xF7F7F7] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.cityArray[indexPath.row].status != 2) {
        if (self.didSelectChosedBlock) {
            self.didSelectChosedBlock(self.cityArray[indexPath.row]);
        }
    }
    
    [self removeFromSuperview];
}

- (IBAction)tapBackCloseAction:(UITapGestureRecognizer *)sender {
    [self hideAnimation];
}
#pragma mark - 隐藏动画
- (void)hideAnimation
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (self.cancelCityViewBlock) {
            self.cancelCityViewBlock();
        }
        [self removeFromSuperview];
    }];
}


- (IBAction)closeButtonDidClick:(id)sender {
    
    [self hideAnimation];
}

@end
